﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.Status;

namespace SoftwareCenter
{
    public class SmsCenter
    {
        private MessageInterceptor interceptor;
        private string comingNumber;
        private SystemState state;
        public SmsCenter()
        {
            interceptor = new MessageInterceptor();
            interceptor.InterceptionAction = InterceptionAction.NotifyAndDelete;
            interceptor.MessageCondition.CaseSensitive = false;
            interceptor.MessageCondition.Property = Microsoft.WindowsMobile.PocketOutlook.MessageInterception.MessageProperty.Body;
            interceptor.MessageCondition.ComparisonType = Microsoft.WindowsMobile.PocketOutlook.MessageInterception.MessagePropertyComparisonType.StartsWith;
            interceptor.MessageCondition.ComparisonValue = "start"; 
            interceptor.MessageReceived += new MessageInterceptorEventHandler(interceptor_MessageReceived);
            //phone call reiever
            state = new SystemState(SystemProperty.PhoneMissedCall);
            state.Changed += new ChangeEventHandler(state_Changed);
 
        }

        void state_Changed(object sender, ChangeEventArgs args)
        {
            CommandCenter c = new CommandCenter();
            string number = SystemState.PhoneLastIncomingCallerNumber;
            c.ComingNumber = number;
            if (number.Length >= 11)
            {
                SendBackMsg("如果1分钟内无错误提示,则重启成功!", c.ComingNumber);
                if (c.Restart())
                {

                }
                else
                {
                    SendBackMsg("重启失败!", c.ComingNumber);
                }
            }
            else
            {
                bool b1 = c.KillProcess("自动化");
                bool b2 = c.StartProcess("自动化");
                if (b2 == true)
                {
                    SendBackMsg("启动成功!", c.ComingNumber);
                }
                else
                {
                    SendBackMsg("启动失败!", c.ComingNumber);
                }
            }

        }

        void interceptor_MessageReceived(object sender, MessageInterceptorEventArgs e)
        {
            SmsMessage msg = (SmsMessage)e.Message;
            this.comingNumber = msg.From.Name;
            string str = msg.Body;
            OnMsgRecieved(str, msg.From.Name);
          
   
            //MessageProperty.interceptor.MessageCondition=new MessageCondition(new MessageProperty()
        }

        void OnMsgRecieved(string msg, string sender)
        {

            CommandCenter c = new CommandCenter();
            c.ComingNumber = comingNumber;
            string process = msg.Substring(5);
            if (process == "重启")
            {
                SendBackMsg("如果1分钟内无错误提示,则重启成功!", c.ComingNumber);
                if (c.Restart())
                {
                    
                }
                else
                {
                    SendBackMsg("重启失败!", c.ComingNumber);
                }
            }
            else
            {
                bool b1 = c.KillProcess(msg.Substring(5));
                bool b2 = c.StartProcess(msg.Substring(5));
                if (b2 == true)
                {
                    SendBackMsg("启动成功!", c.ComingNumber);
                }
                else
                {
                    SendBackMsg("启动失败!", c.ComingNumber);
                }
            }
        }
        void SendBackMsg(string str, string sender)
        {
            if (sender.Length == 0) return;
            try
            {
                SmsMessage msg = new SmsMessage(sender, str);
                msg.Send();
            }
            catch (Exception a)
            {
                Tool.Log(a.Message);
            }
        }
    }
}
